package com.xiaoshuidi.cloud.module.finance.mq.producer;

import cn.hutool.core.date.DateTime;
import com.xiaoshuidi.cloud.framework.pay.core.client.dto.PayOrderNotifyRespDTO;
import com.xiaoshuidi.cloud.module.finance.dal.dataobject.finance.FinanceMoneyFlowDO;
import com.xiaoshuidi.cloud.module.finance.dal.dataobject.order.PayOrderDO;
import com.xiaoshuidi.cloud.module.finance.mq.PayResultConstants;
import com.xiaoshuidi.cloud.module.finance.mq.message.PayResultMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

/**
 * 支付结果消息通知(成功/失败)
 */
@Slf4j
@Component
public class PayResultProducer {
    @Resource
    private StreamBridge streamBridge;
    @Value("${spring.profiles.active}")
    private String profile;

    public void sendPaySuccessMessage(PayOrderDO order, FinanceMoneyFlowDO financeMoneyFlowDO, PayOrderNotifyRespDTO notify) {
        Long amount = notify.getPayAmount();
//        // FIXME 仅测试用
//        if (!"prod".equals(profile)) {
//            amount = Math.max(order.getAmount(), notify.getPayAmount()) - (order.getDiscount() == null ? 0L : order.getDiscount());
//        }
        streamBridge.send(PayResultConstants.OUTPUT, PayResultMessage.builder()
                        .payOrderId(order.getId())
                .sourceBizId(order.getSourceBizId())
                .sourceBizType(order.getSourceBizType())
                .success(notify.isSuccess())
                .payTime(new DateTime(notify.getSuccessTime()).toJdkDate())
                .payAmount(amount)
                .flowNo(financeMoneyFlowDO.getFlowNo())
                .flowId(financeMoneyFlowDO.getId())
                .tenantId(financeMoneyFlowDO.getTenantId())
                .trader(financeMoneyFlowDO.getTrader())
                .payMethod(financeMoneyFlowDO.getPayMethod())
                .couponDiscount(order.getDiscount())
                .build());
    }

    public void sendPaySuccessMessage(FinanceMoneyFlowDO financeMoneyFlowDO) {
        streamBridge.send(PayResultConstants.OUTPUT, PayResultMessage.builder()
                .sourceBizId(financeMoneyFlowDO.getSourceBizId())
                .sourceBizType(financeMoneyFlowDO.getSourceBizType())
                .success(true)
                .payTime(financeMoneyFlowDO.getPayTime() != null ?
                        new DateTime(financeMoneyFlowDO.getPayTime()).toJdkDate()
                        : (financeMoneyFlowDO.getTradeTime() != null ? new DateTime(financeMoneyFlowDO.getTradeTime()).toJdkDate() : new Date()))
                .payAmount(financeMoneyFlowDO.getTradeAmount().multiply(BigDecimal.valueOf(100)).longValue())
                .flowNo(financeMoneyFlowDO.getFlowNo())
                .flowId(financeMoneyFlowDO.getId())
                .tenantId(financeMoneyFlowDO.getTenantId())
                .trader(financeMoneyFlowDO.getTrader())
                .payMethod(financeMoneyFlowDO.getPayMethod())
                .build());
    }
}
